# 导包
import pymysql
# 定义全局变量
conn=None
cursor=None
# 连接数据库
try:
    conn=pymysql.connect(host="localhost",port=3306,user="root",password="123456",
                     database="api_pymysql_database",charset="utf8")
    # 获取游标对象
    cursor=conn.cursor()
    # 执行sql语句
    data=cursor.execute("select * from areas")
    # 使用游标获取数据
    # 获取第一条数据
    first_data=cursor.fetchone()
    print("first_data:",first_data)
    cursor.rownumber=0
    # 获取前两条数据
    first_second_data=cursor.fetchmany(2)
    print("first_second_data:", first_second_data)
    # 获取全部数据
    cursor.rownumber=0
    all_data=cursor.fetchall()
    print("all_data:", all_data)
    cursor.rownumber=2
    # 获取第三条数据和第四条数据
    third_fourth_data=cursor.fetchmany(2)
    print("third_fourth_data:", third_fourth_data)
except Exception as error:
    print("连接数据库失败：", error)
finally:
    print("连接数据库成功")
    # 关闭游标
    cursor.close()
    # 关闭连接
    conn.close()